#include<bits/stdc++.h>
using namespace std;
int a[200005];int n;
void solve1(){
	pair<int,int> pos,neg;
	int ans=0;
	pos={1,a[1]};
	//cout<<"pos ";
	for(int i=2;i<=n;i++){
		if(a[i]==pos.second) {
			//cout<<"pos ";
			pos={i,a[i]};
			ans+=a[i];
		}
		else{
			if(a[i]==neg.second){
				//cout<<"neg ";
				neg={i,a[i]};
				ans+=a[i];
			}
			else{
				//cout<<"und ";
				if(pos.second>neg.second){
					//cout<<"neg ";
					neg={i,a[i]};
				}
				else if(pos.second<neg.second){
					//cout<<"pos ";
					pos={i,a[i]};
				}
				//und={i,a[i]};
			}
		}
	}
	//cout<<endl;
	cout<<ans<<endl;
}
void solve2(){
	int mx=-1,ans=0;
	for(int i=0;i<(1<<(n-1));i++){
		int lpos=0,lneg=0;ans=0;
		for(int j=1;j<=n;j++){
			//cout<<(i&(1<<(j-1)))<<' ';
			if(i&(1<<(j-1)))
				if(lpos==a[j]) ans+=a[j],lpos=a[j];
				else lpos=a[j];
			else 
				if(lneg==a[j]) ans+=a[j],lneg=a[j];
				else lneg=a[j];
		}
		//cout<<endl<<ans<<endl;
		mx=max(mx,ans);
	}
	cout<<mx<<endl;
}
void solve(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	if(n<=20) solve2();
	else{
		solve1();
	}
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int t;cin>>t;
	while(t--) solve();
}
